package com.example.readbook.Adapter

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import com.bifan.txtreaderlib.utils.FormatUtil
import com.example.readbook.Bean.JournalBean
import com.example.readbook.R
import com.example.readbook.Sql.JournalUtils
import com.example.readbook.UI.Act.JournalDetailsActivity
import kotlinx.android.synthetic.main.item_read_journal.view.*

/**
 * Package    : com.example.readbook.Adapter
 * ClassName  : ReadJournalAdapter
 * Description: $读后感列表适配器
 * Date       : 2019/3/26 14:41
 */
class ReadJournalAdapter(val context: Context, var mList: MutableList<JournalBean>) : BaseAdapter<JournalBean>(mList) {
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyHolder {
        return RecyHolder(LayoutInflater.from(context).inflate(R.layout.item_read_journal, p0, false))
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: RecyHolder, p1: Int) {
        val bean = mList[p1]
        holder.itemView.Item_Read_Title.text = "读《" + bean.name + "》有感"
        holder.itemView.Item_Read_data.text = bean.data
        holder.itemView.Item_Read_Time.text = FormatUtil.FormatTime(bean.time.toLong(), "yy-MM-dd HH:mm")
        holder.itemView.Item_Read_Gen.setOnClickListener {
            val intent = Intent(context, JournalDetailsActivity::class.java)
            intent.putExtra("bean", bean)
            context.startActivity(intent)
        }
        holder.itemView.Item_Read_Gen.setOnLongClickListener {
            JournalUtils.getInstence(context).delete(bean.id)
            remove(p1) {}
            true
        }
    }
}